<?php
class Xigg_Main_EditNodeForm extends Sabai_Controller
{
    function _doExecute(&$context)
    {
        if ((!$node =& $this->_parent->getNodeById($context->request, 'node_id')) || !$node->isReadable($context->user)) {
            $context->response->setError(_('Invalid node'), Sabai_URL::get());
            return;
        }
        $node_form =& $node->toForm('Node_edit');
        $node_form->removeElements(array('Tags', 'status'));
        if (!$context->user->isInRole(array(XIGG_ROLE_ADMIN, XIGG_ROLE_EDITOR))) {
            if (!$node->get('allow_edit')) {
                $context->response->setError(_('This news item has been frozen by the administration'), Sabai_URL::get('/node/' . $node->getId()));
                return;
            }
            if ($node->isPublished() && !$context->user->isInRole(XIGG_ROLE_WRITER)) {
                $context->response->setError(_('You are not allowed to edit published news items'), Sabai_URL::get('/node/' . $node->getId()));
                return;
            }
            if (!$node->isOwnedBy($context->user)) {
                $context->response->setError(_('You are not allowed to edit other news items'), Sabai_URL::get('/node/' . $node->getId()));
                return;
            }
            // remove elements user/writer are not allowed to modify
            $node_form->removeElements(array('teaser_html', 'body_html', 'source_title', 'allow_edit', 'priority', 'hidden', 'views'));
            if (!$context->user->isInRole(XIGG_ROLE_WRITER)) {
                $node_form->removeElements(array('allow_comments', 'allow_trackbacks'));
            }
        }
        $plugin_mngr =& $this->_locator->getService('PluginManager');
        if ($context->request->isPost()) {
            $node_form->setValues($context->request->getAll());
            if ($context->request->getAsStr('change_syntax')) {
                // need to convert encoding since AJAX with Form.serialize() comes with UTF-8
                if (SABAI_CHARSET != 'UTF-8') {
                    foreach (array('title', 'teaser', 'body', 'teaser_html', 'body_html', 'source_title', 'Tag') as $node_form_k) {
                        if ($node_form->hasElement($node_form_k)) {
                            $node_form->setValueFor($node_form_k,
                                                    mb_convert_encoding($node_form->getValueFor($node_form_k),
                                                    SABAI_CHARSET,
                                                    array(SABAI_CHARSET, 'UTF-8')));
                        }
                    }
                }
            } elseif ($context->request->getAsStr('preview_form')) {
                $node_form->validate();
                if ($node_form->isValid()) {
                    $node->applyForm($node_form);
                    if ($context->user->isInRole(array(XIGG_ROLE_ADMIN, XIGG_ROLE_EDITOR))) {
                        if ($context->request->getAsBool('teaser_html_regenerate')) {
                            $node->HTMLizeTeaser($plugin_mngr);
                        } else {
                            $context->response->setVar('teaser_html_regenerate', false);
                        }
                        if ($context->request->getAsBool('body_html_regenerate')) {
                            $node->HTMLizeBody($plugin_mngr);
                        } else {
                            $context->response->setVar('body_html_regenerate', false);
                        }
                        $node->purifyHTML($purifier =& $this->_locator->getService('HTMLPurifier'));
                        // set purified HTML back to the form
                        $node_form->setValueFor('teaser_html', $node->get('teaser_html'));
                        $node_form->setValueFor('body_html', $node->get('body_html'));
                    } else {
                        $node->HTMLize($plugin_mngr);
                        $node->purifyHTML($purifier =& $this->_locator->getService('HTMLPurifier'));
                    }
                    $context->response->setVarRef('node_preview', $node);
                }
            } elseif ($context->request->getAsStr('submit_form')) {
                $node_form->validate();
                if ($node_form->isValid()) {
                    $node->applyForm($node_form);
                    $plugin_mngr->dispatch('SubmitNode', array(&$node, /*$isEdit*/ true));
                    if ($context->user->isInRole(array(XIGG_ROLE_ADMIN, XIGG_ROLE_EDITOR))) {
                        if ($context->request->getAsBool('teaser_html_regenerate')) {
                            $node->HTMLizeTeaser($plugin_mngr);
                        }
                        if ($context->request->getAsBool('body_html_regenerate')) {
                            $node->HTMLizeBody($plugin_mngr);
                        }
                    } else {
                        $node->HTMLize($plugin_mngr);
                    }
                    // always purify HTML before commit
                    $node->purifyHTML($this->_locator->getService('HTMLPurifier'));
                    if ($node->commit()) {
                        // do auto tagging after success
                        $node->unlinkTags();
                        if ($tagging = $node_form->getValueFor('tagging')) {
                            $this->_parent->linkTagsToNode($node, $tagging);
                        }
                        $context->response->setSuccess(_('Node updated successfully'), Sabai_URL::get('/node/' . $node->getId()));
                        $plugin_mngr->dispatch('SubmitNodeSuccess', array(&$node, /*$isEdit*/ true));
                        return;
                    }
                }
            }
        }
        $node_form->onView();
        $plugin_mngr->dispatch('ShowNodeForm', array(&$node_form, /*$isEdit*/true));
        $context->response->setVars(array('node_form' => &$node_form,
                                 'node'      => &$node));
    }
}